#include "fsxwsconfiguredialog.h"
#include "ui_fsxwsconfiguredialog.h"
using namespace FsxWsGUI;

//
// Constructor
//
FsxWsConfigureDialog::FsxWsConfigureDialog(QWidget *parent) : QDialog(parent), ui(new Ui::FsxWsConfigureDialog)
{
    ui->setupUi(this);
}

//
// Destructor
//
FsxWsConfigureDialog::~FsxWsConfigureDialog()
{
    delete ui;
}

//
// Set the settings
//
void FsxWsConfigureDialog::setSettings(FsxWsSettings *settings)
{
    if(!settings) return;
    FsxWsConfigureDialog::_settings = settings;
    int periodIndex = 0;
    switch (settings->getFsxUpdatePeriod()) {
    case SIMCONNECT_PERIOD_SECOND:
    {
        periodIndex = 0;
        break;
    }
    case SIMCONNECT_PERIOD_ONCE:
    {
        periodIndex = 3;
        break;
    }
    case SIMCONNECT_PERIOD_NEVER:
    {
        periodIndex = 4;
        break;
    }
    case SIMCONNECT_PERIOD_VISUAL_FRAME:
    {
        periodIndex = 1;
        break;
    }
    case SIMCONNECT_PERIOD_SIM_FRAME:
    {
        periodIndex = 2;
        break;
    }
    }
    ui->qcbUpdatePeriod->setCurrentIndex(periodIndex);
    ui->qdsAircraftRadius->setValue(settings->getFsxAircraftRadiusKilometer());
    ui->qdsMaxWaypoints->setValue(settings->getFsxModelMaxWaypoints());
    ui->qcbMaxWaypoints->setChecked(settings->isFsxModelMaxWaypointsDisabled());
    ui->qcbDumpStatistics->setChecked(settings->isFsxDumpStatisticsEnabled());
    ui->qdsTimeframeWaypoints->setValue(settings->getFsxModelWaypointsTimeframeS());
    ui->qleFsxConnectionIp->setText(HttpHelper::formatIPAddress(settings->getFsxIpAddress(), true));
    ui->qleFsxConnectionPort->setText(QString::number(settings->getFsxIpPort()));
    ui->qcbTimeframeWaypoints->setChecked(settings->isFsxModelWaypointsTimeframeDisabled());
    ui->qcbOnlyShowOwnAircraft->setChecked(settings->getGuiShowOnlyOwnAircraft());
    ui->qdsPollTimeForList->setValue(settings->getGuiUpdateAircraftsS());
    ui->qcbShowWaypoints->setChecked(settings->guiShowWaypointsWhenAircraftSelected());
    ui->qleLineStyleColor->setText(settings->getKmlLineStyleColor());
    ui->qlePolyStyleColor->setText(settings->getKmlPolyStyleColor());
    ui->qdsLineStyleWidth->setValue(settings->getKmlLineStyleWidth());
    ui->qleWaypointModelBaseUrl->setText(settings->getKmlWaypointModelBaseUrl());
    ui->qleWaypointModel->setText(settings->getKmlWaypointModel());
    ui->qcbModelLocalFile->setChecked(settings->getKmlWhenSavedUseLocalModel());
    ui->qcbModelLocalFullPath->setChecked(settings->getKmlWhenSavedUseFullPath());
    ui->qdsKmlNetworkLinkPeriod->setValue(settings->getKmlNetworkLinkUpdatePeriodS());
    ui->qcbAutoConnectFsx->setChecked(settings->isAutoConnectFsx());
    ui->qcbAutoStartWebServer->setChecked(settings->isAutoStartWebServer());
    ui->qleFmcDataDirectory->setText(settings->getFmcDataDirectory());

    // from the web server settings
    int wsPort;
    bool wsData;
    if (wsData = FsxWebServiceHelper::getEditableSettings(wsPort)) {
        ui->qdsWebServerPort->setValue(wsPort);
    }
    ui->qdsWebServerPort->setEnabled(wsData);
    QString wsConfigFile = FsxWebServiceHelper::getConfigFile();
    ui->qteWebServerConfigFile->setText(wsConfigFile);
}

//
// Current tab
//
void FsxWsConfigureDialog::setTab(int tab)
{
    ui->qtwMainTabs->setCurrentIndex(tab);
}

//
// Update the settings
//
void FsxWsConfigureDialog::updateSettings()
{
    QString periodS = ui->qcbUpdatePeriod->currentText();
    SIMCONNECT_PERIOD period = SIMCONNECT_PERIOD_NEVER;
    if(periodS.startsWith("1"))
        period = SIMCONNECT_PERIOD_SECOND;
    else if(periodS.startsWith("v", Qt::CaseInsensitive))
        period = SIMCONNECT_PERIOD_VISUAL_FRAME;
    else if(periodS.startsWith("f", Qt::CaseInsensitive))
        period = SIMCONNECT_PERIOD_SIM_FRAME;
    else if(periodS.startsWith("n", Qt::CaseInsensitive))
        period = SIMCONNECT_PERIOD_NEVER;
    else if(periodS.startsWith("o", Qt::CaseInsensitive))
        period = SIMCONNECT_PERIOD_ONCE;

    FsxWsConfigureDialog::_settings->setFsxUpdatePeriod(period);
    FsxWsConfigureDialog::_settings->setFsxAircraftRadiusKilometer(ui->qdsAircraftRadius->value());
    FsxWsConfigureDialog::_settings->setGuiShowOnlyOwnAircraft(ui->qcbOnlyShowOwnAircraft->isChecked());
    FsxWsConfigureDialog::_settings->setFsxModelMaxWaypoints(ui->qdsMaxWaypoints->value());
    FsxWsConfigureDialog::_settings->setFsxModelWaypointsTimeframeS(ui->qdsTimeframeWaypoints->value());
    FsxWsConfigureDialog::_settings->setFsxModelMaxWaypointsDisabled(ui->qcbMaxWaypoints->isChecked());
    FsxWsConfigureDialog::_settings->setFsxDumpStatistics(ui->qcbDumpStatistics->isChecked());
    FsxWsConfigureDialog::_settings->setFsxModelWaypointsTimeframeDisabled(ui->qcbTimeframeWaypoints->isChecked());
    FsxWsConfigureDialog::_settings->setGuiUpdateAircraftsS(ui->qdsPollTimeForList->value());
    FsxWsConfigureDialog::_settings->setGuiShowWaypointsWhenAircraftSelected(ui->qcbShowWaypoints->isChecked());
    FsxWsConfigureDialog::_settings->setKmlLineStyleColor(ui->qleLineStyleColor->text());
    FsxWsConfigureDialog::_settings->setKmlPolyStyleColor(ui->qlePolyStyleColor->text());
    FsxWsConfigureDialog::_settings->setKmlLineStyleWidth(ui->qdsLineStyleWidth->value());
    FsxWsConfigureDialog::_settings->setKmlWaypointModel(ui->qleWaypointModel->text());
    FsxWsConfigureDialog::_settings->setKmlWhenSavedUseLocalModel(ui->qcbModelLocalFile->isChecked());
    FsxWsConfigureDialog::_settings->setKmlWhenSavedUseFullPath(ui->qcbModelLocalFullPath->isChecked());
    FsxWsConfigureDialog::_settings->setKmlNetworkLinkUpdatePeriodS(ui->qdsKmlNetworkLinkPeriod->value());
    FsxWsConfigureDialog::_settings->setFsxIpAddress(ui->qleFsxConnectionIp->text());
    FsxWsConfigureDialog::_settings->setFsxIpPort(ui->qleFsxConnectionPort->text().toInt());
    FsxWsConfigureDialog::_settings->setAutoConnectFsx(ui->qcbAutoConnectFsx->isChecked());
    FsxWsConfigureDialog::_settings->setAutoStartWebServer(ui->qcbAutoStartWebServer->isChecked());
    FsxWsConfigureDialog::_settings->setFmcDataDirectory(ui->qleFmcDataDirectory->text());
    FsxWsConfigureDialog::_settings->write();

    // init the settings for KML
    JsonMarkupHelper::Settings = FsxWsConfigureDialog::_settings->toKmlSettings();

    // update the WebServer settings
    FsxWebServiceHelper::updateSettings((int)ui->qdsWebServerPort->value());
}

//
// Update / Reset the settings (values)
//
void FsxWsConfigureDialog::onOkCancelClicked(QAbstractButton* button)
{
    QString t = button->text();
    if (t.startsWith("o", Qt::CaseInsensitive))
        FsxWsConfigureDialog::updateSettings();
    else
        FsxWsConfigureDialog::setSettings(FsxWsConfigureDialog::_settings);
}

//
// Check the FMC directory
//
void FsxWsConfigureDialog::onFmcDataDirectoryEditFinished()
{
    QString directory = ui->qleFmcDataDirectory->text();
    // "border: 1px solid green"
    if (Utils::isValidFmcDataDirectory(directory)) {
        ui->qleFmcDataDirectory->setStyleSheet("");
    } else {
        ui->qleFmcDataDirectory->setStyleSheet("background: red");
    }
}
